/**
 * @author xuzj
 */
CQCIS.tree.ColumnNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
	focus : Ext.emptyFn, // prevent odd scrolling behavior
	onClick : function(e) {
		if (this.dropping) {
			e.stopEvent();
			return;
		}
		if (this.fireEvent("beforeclick", this.node, e) !== false) {
			if (!this.disabled && this.node.attributes.href) {
				this.fireEvent("click", this.node, e);
				return;
			}
			e.preventDefault();
			if (this.disabled) {
				return;
			}

			if (this.node.attributes.singleClickExpand && !this.animating && this.node.hasChildNodes()) {
				this.node.toggle();
			}

			this.fireEvent("click", this.node, e);
		} else {
			e.stopEvent();
		}
	},
	onSelectedChange: function(state){
        if (state) {
            this.checkbox.checked = true;
            this.addClass("x-tree-selected");
        } else {
            this.checkbox.checked = false;
            this.removeClass("x-tree-selected");
        }
        this.focus();
    },
	onDblClick : function(e) {
		e.preventDefault();
		if (this.disabled) {
			return;
		}
		if (!this.animating && this.node.hasChildNodes()) {
			this.node.toggle();
		}
		this.fireEvent("dblclick", this.node, e);
	},
	onCheckChange: function(e){
        if (this.fireEvent("beforeclick", this.node, e) !== false) {
            if (this.checkbox.checked) {
                this.node.select();
            } else {
                this.node.unselect();
            }
        } else {
            e.stopEvent();
        }
    },
	renderElements : function(n, a, targetNode, bulkRender) {
		this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';
		var t = n.getOwnerTree();
		var cols = t.columns;
		var bw = 2;
		var c = cols[0];
		if (c.id) {
			this.node.id = a[c.id];
        }
		var flag = false;
		var checkedRows = this.node.ownerTree.checkedRows;
		if (checkedRows) {
			for (var i = 0;i < checkedRows.length; i++) {
				if (checkedRows[i][cols[0].id] == a[cols[0].id]) {
					flag = true;
					break;
				}
			}
		}
		var buf = [
				'<li class="x-tree-node"><div ext:tree-node-id="',
				n.id,
				'" class="x-tree-node-el x-tree-node-leaf ',
				a.cls,
				'">',
				'<div class="x-tree-col" style="width:',
				c.width - bw,
				'px;">',
				'<input class="x-tree-node-cb" type="checkbox" value="',
				this.node.id,
				'"',
				' style="display:',
				this.node.ownerTree.checkbox ? "inline" : "none",
				';"',
				flag ? 'checked' : '',
				' />',
				'<span class="x-tree-node-indent">',
				this.indentMarkup,
				"</span>",
				'<img src="',
				this.emptyIcon,
				'" class="x-tree-ec-icon x-tree-elbow">',
				'<img src="',
				a.icon || this.emptyIcon,
				'" class="x-tree-node-icon',
				(a.icon ? " x-tree-node-inline-icon" : ""),
				(a.iconCls ? " " + a.iconCls : ""),
				'" unselectable="on">',
				'<a hidefocus="on" class="x-tree-node-anchor" href="',
				a.href ? a.href : "#",
				'" tabIndex="1" ',
				a.hrefTarget ? ' target="' + a.hrefTarget + '"' : "",
				'>',
				'<span unselectable="on">',
				n.text || (c.renderer ? c.renderer(a[c.dataIndex], n, a) : a[c.dataIndex]),
				"</span></a>", "</div>"];
		for (var i = 1, len = cols.length;i < len; i++) {
			c = cols[i];
			buf.push(
				'<div class="x-tree-col ',
				(c.cls ? c.cls : ''),
				'" style="width:',
				c.width - bw,
				'px;">',
				'<div class="x-tree-col-text">',
				'<a hidefocus="on" class="x-tree-node-anchor" href="',
				a.href ? a.href : "#",
				'" tabIndex="1" ',
                a.hrefTarget ? ' target="' + a.hrefTarget + '"' : "", '>',
				'<span unselectable="on">',
				(c.renderer ? c.renderer(a[c.dataIndex], n, a) : a[c.dataIndex]),
				"</span></a>", "</div>", "</div>");
       }
		buf.push(
			'<div class="x-clear"></div></div>',
			'<ul class="x-tree-node-ct" style="display:none;"></ul>',
			"</li>");

		if (bulkRender !== true && n.nextSibling && n.nextSibling.ui.getEl()) {
			this.wrap = Ext.DomHelper.insertHtml("beforeBegin", n.nextSibling.ui.getEl(), buf.join(""));
		} else {
			this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf.join(""));
		}
		this.elNode = this.wrap.childNodes[0];
		this.ctNode = this.wrap.childNodes[1];
		var cs = this.elNode.firstChild.childNodes;
		this.checkbox = cs[0];
		this.indentNode = cs[1];
		this.ecNode = cs[2];
		this.iconNode = cs[3];
		this.anchor = cs[4];
		this.textNode = cs[4].firstChild;
		if (flag) {
			this.node.select();
		}
	}
});
